package edu.bu.cs673.superkids123.children.view.games;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.controller.games.SubtractionGameController;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;


/**
 * Class SubtractionGameView
 * 
 * This is the view class which presents math subtraction questions for a 
 * young child to test their aptitude for basic subtraction. The quiz 
 * consists of 5 questions where one of 4 provided answers is the correct one.
 * 
 * @author David Bardwell
 *
 */

public class SubtractionGameView extends AbstractNavigationView implements ActionListener {

	private JButton numberButton1;
	private JButton numberButton2;
	private JButton minusButton;
	private JButton equalsButton;
	private JPanel subtractionPanel;
	private AnswerButton answerButtons[];
	private JLabel instructionLabel;
	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
	private SubtractionGameController controller;
	
	public SubtractionGameView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == numberButton1 || e.getSource() == numberButton2) {
			int numberToPlay = 1;
			if (e.getSource() == numberButton1) {
				numberToPlay = controller.getNextSubtractionQuestion()[0];
			}
			else {
				numberToPlay = controller.getNextSubtractionQuestion()[1];
			}
			controller.playAudioForNumber(numberToPlay);
		}
		else if (e.getSource() == minusButton) {
			controller.playAudioMinus();
		}
		else if (e.getSource() == equalsButton) {
			controller.playAudioEquals();
		}
		else if (e.getSource() instanceof AnswerButton) {
			AnswerButton answerButton = (AnswerButton) e.getSource();
			controller.answerButtonPressed(this, answerButton.index);
		}
	}

	@Override
	public String getTitle() {
		return "Subtraction Game";
	}

	@Override
	public String getBorderTitle() {
		return "Subtraction Game";
	}

	@Override
	protected JComponent layout() {
		controller = getMainFrame().getController(SubtractionGameController.class);
		controller.resetForNextGame();
		
		subtractionPanel = new JPanel();
		subtractionPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridheight = 1;
		c.gridwidth = 1;
		c.weightx = 1;
		c.weighty = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.NORTHWEST;
		
		JPanel equationPanel = new JPanel();
		equationPanel.setLayout(new FlowLayout());
		
		// setup equation panel
		numberButton1 = new JButton();
		numberButton2 = new JButton();
		numberButton1.addActionListener(this);
		numberButton2.addActionListener(this);
		minusButton = new JButton();
		equalsButton = new JButton();
		minusButton.addActionListener(this);
		equalsButton.addActionListener(this);
		JButton questionMarkButton = new JButton();
		
		ImageIcon image10 = provider.getScaledImage(ChildrenResource.imageNumber10, 0.75f);
		ImageIcon image5 = provider.getScaledImage(ChildrenResource.imageNumber5, 0.75f);
		ImageIcon minusImage = provider.getScaledImage(ChildrenResource.minusSign, 0.75f);
		ImageIcon equalsImage = provider.getScaledImage(ChildrenResource.equalsSign, 0.75f);
		ImageIcon questionImage = provider.getScaledImage(ChildrenResource.imageQuestionMark, 0.75f);
		
		numberButton1.setIcon(image10);
		numberButton2.setIcon(image5);
		minusButton.setIcon(minusImage);
		equalsButton.setIcon(equalsImage);
		questionMarkButton.setIcon(questionImage);
		
		equationPanel.add(numberButton1);
		equationPanel.add(minusButton);
		equationPanel.add(numberButton2);
		equationPanel.add(equalsButton);
		equationPanel.add(questionMarkButton);
		
		subtractionPanel.add(equationPanel, c);
		
		JPanel answerSelectionPanel = constructAnswerSelectionPanel();
		c.anchor = GridBagConstraints.WEST;
		c.gridy++;
		c.fill = GridBagConstraints.BOTH;
		subtractionPanel.add(answerSelectionPanel, c);
		
		c.gridy++;
		instructionLabel = new JLabel("Select the correct answer");
		c.anchor = GridBagConstraints.SOUTHWEST;
		subtractionPanel.add(instructionLabel, c);
		
		// show the first question and possible answers for the game
		displayNextQuestionAndAnswersLocal(1, 
				controller.getNextSubtractionQuestion(), 
				controller.getNextSubtractionAnswers());
		
		// push panel elements to the top
		c.weighty = 1;
		c.gridy++;
		subtractionPanel.add(new JPanel(), c);
		
		return subtractionPanel;
	}
	
	private JPanel constructAnswerSelectionPanel() {
		JPanel answerPanel = new JPanel();
		answerPanel.setLayout(new FlowLayout());
		
		answerButtons = new AnswerButton[4];
		
		for (int i = 0; i < 4; i++) {
			answerButtons[i] = new AnswerButton(i);
			answerButtons[i].setSize(new Dimension(150, 150));
			answerButtons[i].setIcon(provider.getScaledImage(ChildrenResource.imageNumber1, 0.66f));
			answerButtons[i].addActionListener(this);
			answerPanel.add(answerButtons[i]);
		}
		return answerPanel;
	}
	
	
	/**
	 *   The controller will call this method to show the next question and answers
	 * @param currentQuestion
	 * @param question
	 * @param answers
	 */
	public void displayNextQuestionAndAnswers(int currentQuestion, int[] question, int[] answers) {
		displayNextQuestionAndAnswersLocal(currentQuestion, question, answers);
		subtractionPanel.revalidate();
		subtractionPanel.repaint();
	}
	
	// Show the next question and possible answers
	private void displayNextQuestionAndAnswersLocal(int currentQuestion, int[] question, int[] answers) {
		ImageIcon numberImage1 = controller.getImageForNumber(question[0], 0.75f);
		numberButton1.setIcon(numberImage1);
		
		ImageIcon numberImage2 = controller.getImageForNumber(question[1], 0.75f);
		numberButton2.setIcon(numberImage2);
		
		ImageIcon answerImages[] = new ImageIcon[4];
		for (int i = 0; i < 4; i++) {
			answerImages[i] = controller.getImageForNumber(answers[i], 0.66f);
			answerButtons[i].setIcon(answerImages[i]);
		}
	}
	
	
	public void updateMessage(String newMessage) {
		instructionLabel.setText(newMessage);
	}
	
	@Override
	public boolean cleanupView() {
		getMainFrame().getController(SubtractionGameController.class)
			.endGame(false);
		
		return true;
	}

	
	/**
	 * The AnswerButtons will be a JButton with an index to keep
	 * track of which answer button has been selected. 
	 * 
	 * @author David Bardwell
	 *
	 */
	private class AnswerButton extends JButton {
		private static final long serialVersionUID = 1L;
		public AnswerButton(int index) {
			super();
			this.index = index;
		}
		public int index;
	}
	
}
